package org.aaron.demos;

import org.aaron.demos.cp.FirstProviderMetaData;
import org.aaron.demos.cp.FirstProviderMetaData.UserTableMetaData;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContentProviderActivity extends Activity {
	private Button cp_insert_btn, cp_query_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.contentproviderlayout);
		initActivity();
	}

	private void initActivity() {
		cp_insert_btn = (Button) this.findViewById(R.id.cp_insert_btn);
		cp_query_btn = (Button) this.findViewById(R.id.cp_query_btn);
		cp_insert_btn.setOnClickListener(new CpInsertClickListener());
		cp_query_btn.setOnClickListener(new CpQueryClickListener());
	}

	class CpInsertClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			ContentValues contentValues = new ContentValues();
			contentValues.put(FirstProviderMetaData.UserTableMetaData.USER_NAME, "aaron");
			Uri uri = getContentResolver().insert(UserTableMetaData.CONTENT_URI, contentValues);
			Log.d("URI", uri.toString());
		}
	}

	class CpQueryClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Cursor cursor = getContentResolver().query(UserTableMetaData.CONTENT_URI, null, null, null, null);
			while (cursor.moveToNext()) {
				Log.d("cursor record:", cursor.getString(cursor.getColumnIndex(UserTableMetaData.USER_NAME)));
			}
		}
	}
}
